import { CoolController, BaseController } from '@cool-midway/core';
import { Get, Inject, Query } from '@midwayjs/core';
import { PayBalanceEntity } from '../../entity/balance';
import { PayBalanceService } from '../../service/balance';

/**
 * 支付模块-用户端余额控制器
 */
@CoolController({
  api: [],
  entity: PayBalanceEntity,
})
export class AppPayBalanceController extends BaseController {
  @Inject()
  ctx;

  @Inject()
  payBalanceService: PayBalanceService;

  /**
   * 获取用户余额
   */
  @Get('/info')
  async getUserBalance() {
    const userId = this.ctx.user.id;
    const balance = await this.payBalanceService.getUserBalance(userId);
    return this.ok(balance);
  }

  /**
   * 获取余额变动记录
   */
  @Get('/logs')
  async getBalanceLogs(@Query() query: { 
    page?: number; 
    size?: number; 
    changeType?: number; 
    balanceType?: number 
  }) {
    const { page = 1, size = 10, changeType, balanceType } = query;
    const userId = this.ctx.user.id;

    const result = await this.payBalanceService.getBalanceLogs(
      userId, 
      page, 
      size, 
      changeType, 
      balanceType
    );
    return this.ok(result);
  }
}